/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.view;

import com.eng.scc.core.data.Address;
import com.eng.scc.core.data.Company;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.details.CompanyDetails;
import com.eng.scc.core.editors.CompanyEditor;
import com.eng.scc.core.parts.AbstractApplicationAction;
import com.eng.scc.core.parts.ActionCategory;
import com.eng.scc.core.parts.CompaniesList;
import com.eng.scc.core.parts.SummaryPane;
import com.eng.scc.core.util.HTMLFactory;
import com.eng.scc.core.util.ImageFinder;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Rafael
 */
public class CompanyApplicationView extends ApplicationView
{

    public CompanyApplicationView()
    {
        initComponents();
    }

    @Override
    public void refreshContent()
    {
        try
        {
            companiesList.setCompanies( FuelControllerManagerService.getInstance().getCompanies() );

            if ( companiesList.getSelectedCompany() != null )
            {
                summaryPane.setContent( CompanyDetails.getDetails( companiesList.getSelectedCompany() ) );
            }

            else
            {
                summaryPane.setContent( HTMLFactory.Body.EMPTY );
            }

            revalidate();
            repaint();
        }

        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    private void find()
    {
//        try
//        {
//            if ( ! tfSearch.getText().isEmpty() )
//            {
//                clientsTable.setClients( DatabaseManagerService.getInstance().findByName( tfSearch.getText() ) );
//            }
//            
//            else
//            {
//                refreshContent();
//            }
//        }
//        catch ( Exception e )
//        {
//            ApplicationContext.getInstance().saveLog( e );
//        }
    }

    private void initComponents()
    {
        setLayout( new GridBagLayout() );

        btSearch.setIcon( ImageFinder.getImageIcon( "bt_search.png" ) );

        btSearch.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                find();
            }
        } );

        tfSearch.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                find();
            }
        } );

        companiesList.addListSelectionListener( new ListSelectionListener()
        {
            @Override
            public void valueChanged( ListSelectionEvent e )
            {
                if ( companiesList.getSelectedCompany() != null )
                {
                    summaryPane.setContent( CompanyDetails.getDetails( companiesList.getSelectedCompany() ) );
                }

                else
                {
                    summaryPane.setContent( HTMLFactory.Body.EMPTY );
                }
            }
        } );

        companiesList.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseClicked( MouseEvent e )
            {
                if ( e.getClickCount() >= 2 )
                {
                    edit();
                }
            }
        } );

        add( lbSearch,
             new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
                                     GridBagConstraints.WEST,
                                     GridBagConstraints.NONE,
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        add( tfSearch,
             new GridBagConstraints( 2, 0, 1, 1, 1.0, 0.0,
                                     GridBagConstraints.WEST,
                                     GridBagConstraints.HORIZONTAL,
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        add( btSearch,
             new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0,
                                     GridBagConstraints.WEST,
                                     GridBagConstraints.NONE,
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );

        btSearch.addActionListener( new ActionListener()
        {

            @Override
            public void actionPerformed( ActionEvent e )
            {
                findText();
            }

            private void findText()
            {
                try
                {
                    String text = tfSearch.getText();

                    if ( !text.isEmpty() )
                    {
//                       for ( Object object : companiesList.)
//                       {
//                           
//                       }
                    }

                }

                catch ( Exception e )
                {
                    e.printStackTrace();
                }
            }
        } );

        splitPane.setLeftComponent( paneScroll );
        splitPane.setDividerLocation( 0.2 );
        splitPane.setRightComponent( summaryPane );

        add( splitPane,
             new GridBagConstraints( 1, 1, 3, 1, 1.0, 1.0,
                                     GridBagConstraints.NORTH,
                                     GridBagConstraints.BOTH,
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
    }

    private CompaniesList companiesList = new CompaniesList();
    private JScrollPane paneScroll = new JScrollPane( companiesList );
    private JSplitPane splitPane = new JSplitPane();
//    private JPanel panelContent = new JPanel();
    private JLabel lbSearch = new JLabel( "Buscar:" );
    private JTextField tfSearch = new JTextField();

    private SummaryPane summaryPane = new SummaryPane();

    private JButton btSearch = new JButton();

    public void add()
    {
        try
        {
            Company c = new Company();

            if ( CompanyEditor.edit( this, c, new Address() ) )
            {
//                FuelControllerManagerService.getInstance().addCompany( c );

                refreshContent();
            }
        }

        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    public void delete()
    {
        if ( companiesList.getSelectedCompany() != null )
        {
            try
            {
                if ( JOptionPane.showConfirmDialog( this, "Deseja Excluir a Empresa selecionada?" ) == JOptionPane.YES_OPTION )
                {
                    FuelControllerManagerService.getInstance().deleteCompany( companiesList.getSelectedCompany() );

                    refreshContent();
                }
            }

            catch ( Exception e )
            {
                ApplicationContext.getInstance().saveLog( e );
            }
        }

        else
        {
            JOptionPane.showMessageDialog( this, "Selecione uma Empresa!" );
        }
    }

    public void edit()
    {
        try
        {
            if ( companiesList.getSelectedCompany() != null )
            {
                Company c = companiesList.getSelectedCompany();
                Address a = FuelControllerManagerService.getInstance().getAddress( c.getAddressId() );

                if ( CompanyEditor.edit( this, c, a ) )
                {
//                    FuelControllerManagerService.getInstance().updateCompany( c );

                    refreshContent();
                }
            }

            else
            {
                JOptionPane.showMessageDialog( this, "Selecione uma Empresa!" );
            }
        }

        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    @Override
    public List<ActionCategory> getViewActions()
    {
        List<ActionCategory> categorys = new ArrayList<ActionCategory>();
        categorys.add( new ActionCategory( "Empresas", new AbstractApplicationAction[]
        {
            btAdd,
            btEdit,
            btDelete,
            btPrint
        } ) );
        return categorys;
    }

    protected AbstractApplicationAction btPrint = new AbstractApplicationAction( "Imprimir", "tb_print.png" )
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
//            Database.runReport( "full_client_report.jasper" );
        }
    };
}
